package com.minhua.jijiehao2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.minhua.jijiehao2.R;

import java.util.List;

/**
 * 自动垂直滚动;轮播的TextView
 *
 * @author moo
 */
public class AutoVerticalScrollTextView extends TextSwitcher implements ViewSwitcher.ViewFactory {
    public static final int HANDLER_SHOW = 199;


    private Context mContext;
    private List<String> data;
    private int number;
    private int currentIndex = 0;
    /**
     * mInUp,mOutUp分别构成向下翻页的进出动画
     **/
    private Rotate3dAnimation mInUp;
    private Rotate3dAnimation mOutUp;

    public AutoVerticalScrollTextView(Context context) {
        this(context, null);
    }

    public AutoVerticalScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();

    }

    /**
     * 设置需要展示的数据
     */
    public void setData(List<String> list) {
        if (list != null && list.size() > 0) {
            this.data = list;
        }

        if (data != null && data.size() > 0) {
            showData(data);
        }

    }

    /**
     * 展示数据
     */
    private void showData(final List<String> data) {
        this.setText(data.get(0));
        handler.removeCallbacksAndMessages(null);
        handler.sendEmptyMessageDelayed(HANDLER_SHOW, 3000);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == HANDLER_SHOW) {
                next();
                number++;
                AutoVerticalScrollTextView.this.setText(data.get(number % data.size()));
                currentIndex = number % data.size();
                handler.sendEmptyMessageDelayed(HANDLER_SHOW, 3000);
            }
        }
    };

    public int getCurrentIndex() {
        return currentIndex;
    }



    private void init() {

        setFactory(this);

        mInUp = createAnim(true, true);
        mOutUp = createAnim(false, true);

        //当View显示时动画资源ID
        setInAnimation(mInUp);
        //当View隐藏是动画资源ID。
        setOutAnimation(mOutUp);

    }

    private Rotate3dAnimation createAnim(boolean turnIn, boolean turnUp) {

        Rotate3dAnimation rotation = new Rotate3dAnimation(turnIn, turnUp);
        //执行动画的时间
        rotation.setDuration(1200);
        //是否保持动画完毕之后的状态
        rotation.setFillAfter(false);
        //设置加速模式
        rotation.setInterpolator(new AccelerateInterpolator());

        return rotation;
    }


    //这里返回的TextView，就是我们看到的View,可以设置自己想要的效果
    @Override
    public View makeView() {
        TextView textView = new TextView(mContext);
        textView.setTextSize(12);
        textView.setSingleLine(true);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setTextColor(getResources().getColor(R.color.textColorLevel1));
        return textView;

    }

    /**
     * 定义动作，向上滚动翻页
     */
    public void next() {
        //显示动画
        if (getInAnimation() != mInUp) {
            setInAnimation(mInUp);
        }
        //隐藏动画
        if (getOutAnimation() != mOutUp) {
            setOutAnimation(mOutUp);
        }
    }

    /**
     * 动画效果
     */
    class Rotate3dAnimation extends Animation {
        private float mCenterX;
        private float mCenterY;
        private final boolean mTurnIn;
        private final boolean mTurnUp;
        private Camera mCamera;

        Rotate3dAnimation(boolean turnIn, boolean turnUp) {
            mTurnIn = turnIn;
            mTurnUp = turnUp;
        }

        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            mCamera = new Camera();
            mCenterY = getHeight();
            mCenterX = getWidth();
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            final float centerX = mCenterX;
            final float centerY = mCenterY;
            final Camera camera = mCamera;
            final int derection = mTurnUp ? 1 : -1;
            final Matrix matrix = t.getMatrix();

            camera.save();
            if (mTurnIn) {
                camera.translate(0.0f, derection * mCenterY * (interpolatedTime - 1.0f), 0.0f);
            } else {
                camera.translate(0.0f, derection * mCenterY * (interpolatedTime), 0.0f);
            }
            camera.getMatrix(matrix);
            camera.restore();
            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }
}
